package prv.ljw.boot.asyncs;

import com.sun.tools.javadoc.Start;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;

import java.util.Random;
import java.util.concurrent.Future;

/**
 * Created by carry on 2017/3/1.
 */
@Component
public class AsyncsShowTime {

    public static Random random =new Random();

    @Async
    public Future<String> oneStep() throws InterruptedException {
        System.out.println("第一个异步开始");
        long start = System.currentTimeMillis();
        Thread.sleep(random.nextInt(10000));
        long end = System.currentTimeMillis();
        System.out.println("第一个异步结束:用时:"+(end - start));
        return new AsyncResult<>("one ok!");
    }

    @Async
    public Future<String> oneTwo() throws InterruptedException {
        System.out.println("第二个异步开始");
        long start = System.currentTimeMillis();
        Thread.sleep(random.nextInt(10000));
        long end = System.currentTimeMillis();
        System.out.println("第二个异步结束:用时:"+(end - start));
        return new AsyncResult<>("two ok!");
    }

    @Async
    public Future<String> oneThree() throws InterruptedException {
        System.out.println("第三个异步开始");
        long start = System.currentTimeMillis();
        Thread.sleep(random.nextInt(10000));
        long end = System.currentTimeMillis();
        System.out.println("第三个异步结束:用时:"+(end - start));
        return new AsyncResult<>("three ok!");
    }

}
